//
//  RemarkViewController.m
//  NtoccShipper
//
//  Created by 张天兴 on 2024/10/9.
//  Copyright © 2024 zhuchongjin. All rights reserved.
//

#import "RemarkViewController.h"

@interface RemarkViewController ()<UITextViewDelegate>
@property (nonatomic, strong) FlexTextView *tfInPut;
@property (nonatomic, strong) CJLabel *linesLabel;
@property (nonatomic, strong) CJButton *btnSure;
@end

@implementation RemarkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setBackLeftButton:@""];
    [self.view addSubview:self.tfInPut];
    [self.view addSubview:self.linesLabel];
    [self.view addSubview:self.btnSure];
    if (![CJRegular getIsStrEmpty:self.remarkStr]) {
        self.tfInPut.text = self.remarkStr;
        self.tfInPut.placeholder = @"";
        self.linesLabel.text = CJStringWithFormat(@"%lu/50",self.remarkStr.length);
        [self setTitleName:@"修改备注"];
    }else{
        [self setTitleName:@"备注"];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (FlexTextView *)tfInPut{
    if (!_tfInPut) {
        self.tfInPut = [[FlexTextView alloc] initWithFrame:CGRectMake(15, 15 + kStatusBarHeight + kNavBarHeight,SCREEN_WIDTH - 30, 100)];
        self.tfInPut.placeholder = @"请输入备注";
        self.tfInPut.backgroundColor = [UIColor whiteColor];
        self.tfInPut.font = [UIFont systemFontOfSize:16];
        self.tfInPut.layer.masksToBounds = YES;
        self.tfInPut.layer.cornerRadius = 10;
        self.tfInPut.delegate = self;
    }
    return _tfInPut;
}

- (CJLabel *)linesLabel{
    if (_linesLabel == nil) {
        self.linesLabel = [[CJLabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.tfInPut.frame) - 100, CGRectGetMaxY(self.tfInPut.frame), 100, 30)];
        self.linesLabel.text = @"0/50";
        self.linesLabel.textColor = [UIColor cjSubheadTextColor];
        self.linesLabel.textAlignment = NSTextAlignmentRight;
    }
    return _linesLabel;
}
- (CJButton *)btnSure{
    if (!_btnSure) {
        _btnSure = CJButton.btnInit
            .btnFrame(CGRectMake(15 ,SCREEN_HEIGHT - 50 - kTabBarHeight,SCREEN_WIDTH - 30 ,50))
        .btnText(@"确定")
        .btnNormalTitleColor([UIColor cjWhiteColor])
        .btnBgColor([UIColor cjMainColor])
        .btnTargetAction(self, @selector(btnSureAction))
        .btnCornerRadius(10)
        ;
    }
    return _btnSure;
}
- (void)textViewDidChangeSelection:(UITextView *)textView{
    self.linesLabel.text = CJStringWithFormat(@"%lu/50",(unsigned long)self.tfInPut.text.length);
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString *textStr = [self.tfInPut.text stringByReplacingCharactersInRange:range withString:text];
    NSUInteger lines = textStr.length;
    if (lines > 50) {
        [CJAlert showStringDismissWithDelay:@"输入字数不能大于50"];
        return NO;
    }
    return YES;
}
- (void)btnSureAction{
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/updateRemark",CJBaseUrl);
    NSDictionary *dicPar = @{@"code":self.code,
                             @"remark":self.tfInPut.text
    };
    WS(ws);
    [[HDNetworking sharedHDNetworking] jsonTaskWithHTTPMethod:PostType urlString:strURL withParameter:dicPar status:OperationTip success:^(id  _Nonnull responseObject) {
        [CJAlert showStringDismissWithDelay:@"备注成功"];
        [self pop];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
@end
